package com.jh.fcsm.util.word;

import org.apache.poi.xwpf.usermodel.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * 操作word中表格
 */
public class WordTablesUtils {
    /**
     * 替换所有的表格
     *
     * @param xwpfTableList
     * @param params
     */
    public static void replaceInTables(List<XWPFTable> xwpfTableList, Map<String, Object> params) throws Exception {
        if (xwpfTableList == null) {
            return;
        }
        for (XWPFTable table : xwpfTableList) {
            replaceInTable(table, params);
        }
    }

    /**
     * 替换一个表格中的所有行
     *
     * @param xwpfTable
     * @param params
     */
    public static void replaceInTable(XWPFTable xwpfTable, Map<String, Object> params) throws Exception {
        List<XWPFTableRow> rows = xwpfTable.getRows();
        replaceInRows(rows, params);
    }

    /**
     * 替换表格中的一行
     *
     * @param rows
     * @param params
     */
    public static void replaceInRows(List<XWPFTableRow> rows, Map<String, Object> params) throws Exception {
        for (int i = 0; i < rows.size(); i++) {
            XWPFTableRow row = rows.get(i);
            replaceInCells(row.getTableCells(), params);
        }
    }

    /**
     * 替换一行中所有的单元格
     *
     * @param xwpfTableCellList
     * @param params
     */
    public static void replaceInCells(List<XWPFTableCell> xwpfTableCellList, Map<String, Object> params) throws Exception {
        for (XWPFTableCell cell : xwpfTableCellList) {
            replaceInCell(cell, params);
        }
    }

    /**
     * 替换表格中每一行中的每一个单元格中的所有段落
     *
     * @param cell
     * @param params
     */
    public static void replaceInCell(XWPFTableCell cell, Map<String, Object> params) throws Exception {
        List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
        WordParagraphsUtils.replaceInAllParagraphs(cellParagraphs, params);
    }

    /**
     * 填充行
     *
     * @param row
     * @param map
     */
    public static void fillRow(XWPFTableRow row, Map<String, String> map) {
        List<XWPFTableCell> cells = row.getTableCells();
        for (XWPFTableCell cell : cells) {
            List<XWPFParagraph> paragraphs = cell.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String key = run.getText(0);
                    String value = getMapValue(map, key);
                    run.setText(value, 0);
                }
            }
        }
    }

    /**
     * 填充行
     *
     * @param row
     */
    public static void fillBlankRow(XWPFTableRow row) {
        List<XWPFTableCell> cells = row.getTableCells();
        for (XWPFTableCell cell : cells) {
            List<XWPFParagraph> paragraphs = cell.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                for (XWPFRun run : paragraph.getRuns()) {
                    run.setText("", 0);
                }
            }
        }
    }

    /**
     * 获取队列字段
     *
     * @param map
     * @param key
     * @return
     */
    public static String getMapValue(Map<String, String> map, String key) {
        Set<Map.Entry<String, String>> set = map.entrySet();
        for (Map.Entry<String, String> entry : set) {
            if (key.equals("${" + entry.getKey() + "}")) {
                return entry.getValue();
            }
        }
        return "";
    }

}
